<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>创建备份</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script src="/js/xkube.js?v=1" charset="utf-8"></script>
    <style type="text/css">
        .layui-table-cell {
          height: auto;
          line-height: 22px !important;
          text-overflow: inherit;
          overflow: visible;
          white-space: normal;
        }
        .layui-table-cell .layui-table-tool-panel li {
          word-break: break-word;
        }
    </style>
</head>
<body>
<blockquote class="layui-elem-quote layui-font-red">
  注意事项：<br>
    1.创建备份,需要选择正确的集群、资源类型、命名空间以及资源名称<br>
</blockquote>
<div class="layuimini-container">
    <div class="layuimini-main">
      <fieldset class="table-search-fieldset">
        <legend>创建备份</legend>
        <form class="layui-form layui-form-pane" action="" lay-filter="FormInfo">
            <div class="layui-form-item">
                <div class="layui-inline">
                  <label class="layui-form-label layui-font-red">资源类型</label>
                  <div class="layui-input-inline" style="width:150px">
                    <select name="resType" lay-filter="resType" lay-verify="required" lay-search="" id="resType">
                			<option value="" selected="">选择克隆类型</option>
                			<option value="deployment">deployment</option>
                			<option value="service">service</option>
                			<option value="configmap">configmap</option>
                			<option value="secret">secret</option>
                			<option value="cronjob">cronjob</option>
                			<option value="statefulset">statefulset</option>
                      <option value="ingress">ingress</option>
                      <option value="hpa">hpa</option>
    		            </select>
                  </div>
                  <div class="layui-form-mid layui-word-aux layui-font-red">先选择需要备份的资源类型</div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label layui-font-green">当前集群</label>
                    <div class="layui-input-inline" style="width:150px">
                        <select name="clusterId" lay-filter="cluster_Id"  lay-verify="required" lay-search="" id="cluster_Id">
                          <option value="">请选择集群</option>
                        </select>
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label layui-font-green">命名空间</label>
                    <div class="layui-input-inline" style="width:150px">
                      <select name="nameSpace" lay-filter="nameSpace"  lay-verify="required" lay-search="" id="name_Space">
  	              			<option value="">选择命名空间</option>
      		            </select>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label layui-font-green">资源名称</label>
                    <div class="layui-input-inline" style="width:433px">
                      <input type="text" name="resName" placeholder="不能为空" lay-verify="required" lay-reqtext="不能为空" value="" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label layui-font-green">备注</label>
                    <div class="layui-input-inline" style="width:433px">
                      <input type="text" name="remarks" placeholder="" value="" class="layui-input">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">创建备份</button>
                </div>
            </div>
        </form>
      </fieldset>
    </div>
</div>
</body>
<script type="text/javascript">
    	$(document).ready(function(){
          GetCurrClusterId(); 
          GetNamespace();	 
    	});		
</script>

<script>
var clusterId = getQueryString("clusterId");
var nameSpace = getQueryString("nameSpace");
if (clusterId == null) {
	clusterId = getCookie("clusterId")
}
var  cronjobVersion = "";
var  ingressVersion = "";
var  hpaVersion = "";
var clusterIdVersion = getCookie(clusterId);
if ( compareVersions("1.22.0",clusterIdVersion) == 1 ) {
   cronjobVersion = "_beta1";
}
if ( compareVersions("1.20.1",clusterIdVersion) == 1 ) {
   ingressVersion = "_beta1";
}
if ( compareVersions("1.25.0",clusterIdVersion) == 1 ) {
   hpaVersion = "_beta2";
}

    layui.use(['form','table'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            $ = layui.$;

      var resType = getQueryString("resType");
      var resName = getQueryString("resName");
      if ( resType != "" && resType != null ){   
				  form.val('FormInfo', {
				    "resType": resType,
            "nameSpace": nameSpace,
            "resName": resName,
            "clusterId": clusterId
				  })     
      }
        //选择集群
        form.on('select(cluster_Id)', function(data){
          //console.log(data.value);
          clusterIdVersion = getCookie(data.value);
          if ( compareVersions("1.22.0",clusterIdVersion) == 1 ) {
             cronjobVersion = "_beta1";
          }
          if ( compareVersions("1.20.1",clusterIdVersion) == 1 ) {
             ingressVersion = "_beta1";
          }
          if ( compareVersions("1.25.0",clusterIdVersion) == 1 ) {
             hpaVersion = "_beta2";
          }
      		$.get('/xkube/ns/v1/List?clusterId='+data.value, function (resp) {
              console.log(resp);
              $("#name_Space").empty();
              var html = '<option value="" selected="">选择命名空间</option>'
              $("#name_Space").append(html);
        			$.each(resp.data,function(i,item){
        				var html2 = '<option value="'+item.nameSpace+'">'+item.nameSpace+'</option>'
        				$("#name_Space").append(html2);
        			});
        			form.render();		
      	  });	
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
                data.field.resName = data.field.resName.replace(/\s/g,"");
                data.field.remarks = data.field.remarks.replace(/\s/g,"");
                
                if (data.field.resType == "cronjob") {
                  data.field.resType = data.field.resType+cronjobVersion
                }
                if (data.field.resType == "ingress") {
                  data.field.resType = data.field.resType+ingressVersion
                }
                if (data.field.resType == "hpa") {
                  data.field.resType = data.field.resType+hpaVersion
                }
                
			          layer.confirm('确定创建?', {icon: 3, title:'提示',yes: function(index){
                     var index2 = layer.load(0, {shade: false});
                     layer.msg('稍等片刻');
                     $.ajax({
                       url: "/xkube/backup/v1/Backup?clusterId="+data.field.clusterId,
                       type: "post",
                       data: JSON.stringify(data.field),
                       dataType: "json",
                       success: function (resp) {
                            layer.close(index2);
                             if(resp.code == 0){
                                layer.msg('创建完成', {icon:1})
                             }else{
                                layer.msg(resp.msg,{icon:2});
                             }
                        }
                      });		  	  
                  },
                  cancel: function(index, layero){ 
                    layer.close(index);
                    layer.close(index2);
		                console.log("不操作");
                  } 
                });
              return false;
        });

    });
</script>
</html>